﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PiaoanWeb.AppLogic.BusinessLogicLayer;
using PiaoanWeb.AppLogic.DataAccessLayer;
using PiaoanWeb.Helpers;
namespace PiaoanWeb.Admin
{
    public partial class EditNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                NewsBLL newsBLL = new NewsBLL();
                News news = newsBLL.GetNewsByID(Convert.ToInt32(Request.QueryString["NewsID"]));
                titleTxtBox.Text = news.Title;
                contentTxtbox.Text = WebUtility.HtmlDecode(news.Content);
            }
        }

        protected void saveBtn_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(titleTxtBox.Text))
            {
                msgLbl.Text = "标题不能为空";
            }
            else if (string.IsNullOrEmpty(contentTxtbox.Text))
            {
                msgLbl.Text = "内容不能为空";
            }
            else
            {
                try
                {
                    NewsBLL newsBLL = new NewsBLL();
                    News news = newsBLL.GetNewsByID(Convert.ToInt32(Request.QueryString["NewsID"]));
                    news.Title = titleTxtBox.Text;
                    news.Content = contentTxtbox.Text;
                    newsBLL.Save();
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                    msgLbl.Text = "信息添加失败，请联系系统管理员：" + ex.Message;
                }
            }


            Response.Redirect("NewsList.aspx");
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("NewsList.aspx");
        }
    }
}